class_name CPlayermailDataDefine

var m_UUID
var m_tblPlayerSysMail: CPlayerSysMail
var m_tblMailDes: CMailDes


static func GetType():
    return TableCommonDefine.EDataDefineType.eDDT_Playermail


func _init(_uuid, _context):
    m_UUID = _uuid
    self.Reset()


func ClearData():
    # "client none"
    pass


func Reset():
    self.tblPlayerSysMail = {}
    self.tblMailDes = {}


func GetUUID():
    return self.m_UUID



func GetDBDefine():
    return null


func GetDataDefineName():
    return "CPlayermailDataDefine"



func UnPack(_bufferStream, _size, _pos):
    assert( _bufferStream.GetDataSize() == _size )
    _bufferStream.SetPosition( _pos )
    var nCount = 0
    var nUUID = 0

    nCount = _bufferStream.ReadUint16()
    for i in range(nCount):
        nUUID = _bufferStream.ReadUint64()
        var data = self.tblPlayerSysMail[nUUID] or CPlayerSysMail.new( nUUID )
        data.UnPack(_bufferStream)
        self.tblPlayerSysMail[nUUID] = data
    

    nCount = _bufferStream.ReadUint16()
    for i in range(nCount):
        nUUID = _bufferStream.ReadUint64()
        var data = self.tblMailDes[nUUID] or CMailDes.new( nUUID )
        data.UnPack(_bufferStream)
        self.tblMailDes[nUUID] = data
    



func GetTableByTableId(_nTblId):
    # var szObjName = 'tbl' .. AllTables[_nTblId]
    # if self[szObjName] == null :
    #     return null
    
    # return self[szObjName]
    return null


func SynFieldData(_nOperType,_tableID, _nDataUUID, _fieldID,  _dataStream, _nBufferSize, _nPos):

    if _tableID == TableCommonDefine.EOtherDataID.eOD_PlayerSysMail :
        if TableCommonDefine.eCTOp_Update == _nOperType :
            var buffStream = CBufferStream.new(null);
            buffStream.Load(_dataStream, _nPos, _nBufferSize )
            var data = self.GetPlayerSysMail(_nDataUUID)
            return data and data.UnPackField(_fieldID,buffStream);
        elif TableCommonDefine.eCTOp_Insert == _nOperType :
            var buffStream = CBufferStream.new(null);
            buffStream.Load(_dataStream, _nPos, _nBufferSize )
            var data = CPlayerSysMail.new(_nDataUUID)
            data.UnPack(buffStream)
            self.tblPlayerSysMail[_nDataUUID] = data
            return true
        elif TableCommonDefine.eCTOp_Delete == _nOperType :
            self.tblPlayerSysMail[_nDataUUID] = null
            return true
        
        return false
    

    if _tableID == TableCommonDefine.EOtherDataID.eOD_MailDes :
        if TableCommonDefine.eCTOp_Update == _nOperType :
            var buffStream = CBufferStream.new(null);
            buffStream.Load(_dataStream, _nPos, _nBufferSize )
            var data = self.GetMailDes(_nDataUUID)
            return data and data.UnPackField(_fieldID,buffStream);
        elif TableCommonDefine.eCTOp_Insert == _nOperType :
            var buffStream = CBufferStream.new(null);
            buffStream.Load(_dataStream, _nPos, _nBufferSize )
            var data = CMailDes.new(_nDataUUID)
            data.UnPack(buffStream)
            self.tblMailDes[_nDataUUID] = data
            return true
        elif TableCommonDefine.eCTOp_Delete == _nOperType :
            self.tblMailDes[_nDataUUID] = null
            return true
        
        return false
    

    var tbl = self.GetTableByTableId(_tableID)
    if null == tbl :
        return false
    
    if _nOperType == TableCommonDefine.eCTOp_Update:
        var buffStream = CBufferStream.new(null);
        buffStream.Load(_dataStream, _nPos, _nBufferSize)
        return tbl.UnPackField( _fieldID,buffStream, _nBufferSize)
    elif _nOperType == TableCommonDefine.eCTOp_Insert:
        var buffStream = CBufferStream.new(null)
        buffStream.Load(_dataStream, _nPos, _nBufferSize)
        tbl.AddField(_fieldID,buffStream)
        return true
    elif _nOperType == TableCommonDefine.eCTOp_Delete:
        tbl.DelField(_fieldID)
        return true
    elif _nOperType == TableCommonDefine.eCTOp_Assign:
        var buffStream = CBufferStream.new(null)
        buffStream.Load(_dataStream, _nPos, _nBufferSize)
        var nArraySize = buffStream.ReadUint32()
        tbl.SetCurNum(nArraySize)
    
    return false


func AddTableListener(_szTable, _szField, _szCallback, caller):
    var szFunGetTableName = "Get%s" % [ _szTable]
    if not has_method(szFunGetTableName):
        return false
    
    var table = call(szFunGetTableName)
    table.AddEventListener(_szField, _szCallback, caller)


func RemoveTableListener(_szTable, _szField, _szCallback, caller):
    var szFunGetTableName = "Get%s" % [ _szTable]
    if not has_method(szFunGetTableName):
        return false
    
    var table = call(szFunGetTableName)
    table.RemoveEventListener(_szField, _szCallback, caller)

func CopyFrom(_pOther):
    self.m_UUID = _pOther.m_UUID
    for key in _pOther.tblPlayerSysMail:
        var value =  _pOther.tblPlayerSysMail[key]
        var elem = CPlayerSysMail.new(key)
        elem.CopyFrom(value)
        self.tblPlayerSysMail[key] = elem
    
    for key in _pOther.tblMailDes:
        var value =  _pOther.tblMailDes[key]
        var elem = CMailDes.new(key)
        elem.CopyFrom(value)
        self.tblMailDes[key] = elem
    



func GetPlayerSysMail(uuid):    
    return self.tblPlayerSysMail[uuid]

func GetAllPlayerSysMail():
    return self.tblPlayerSysMail

func GetMailDes(uuid):    
    return self.tblMailDes[uuid]

func GetAllMailDes():
    return self.tblMailDes


